import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘国水
 * Date: 2025-05-07
 * Time: 21:13
 */
public class BianaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {

        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);

    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static int nodeSize = 0;

    /**
     * 获取树中节点的个数：遍历思路
     */
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }

        return size2(root.left) + size2(root.right) + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    public void getLeafNodeCount1(TreeNode root) {
        if(root == null) {
            return;
        }

        if(root.left == null && root.right == null) {
            leafSize++;
        }

        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null ) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }

        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    public int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) {
            return 0;
        }

        if(k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH > rightH ? leftH+1 : rightH+1;
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }

        TreeNode ret = find(root.left,val);
        if(ret != null) {
            return ret;
        }
        TreeNode ret2 = find(root.right,val);
        if(ret2 != null) {
            return ret2;
        }
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }

            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

//    public List<List<Integer>> levelOrderBottom(TreeNode root){
//       List<List<Integer>> ret = new ArrayList<>();
//
//       if(root == null) {
//           return ret;
//       }
//
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//
//        while (!queue.isEmpty()) {
//            List<Integer> curRow = new ArrayList<>();
//            int size = queue.size();
//            while (size != 0) {
//                TreeNode cur = queue.poll();
//                curRow.add(cur.val);
//                if(cur.left != null) {
//                    queue.offer(cur.left);
//                }
//
//                if(cur.right != null) {
//                    queue.offer(cur.right);
//                }
//                size--;
//            }
//            ret.add(curRow);
//        }
//        return ret;
//    }


    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        return true;
    }
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        System.out.println(temp);
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }


    public boolean isSameTree(TreeNode p, TreeNode q) {
        if((p != null && q == null ) ||(p == null && q != null)) {
            return false;
        }

        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val) {
            return false;
        }

        return  isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return Math.abs(leftH - rightH) <= 1 && isCompleteTree(root.left) && isCompleteTree(root.right);
    }

    public boolean isBalanced2(TreeNode root) {
        if(root == null) {
            return true;
        }
        return getHeight(root) > 0;
    }
    public int getHeight2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftH = getHeight2(root.left);
        if(leftH < 0) {
            return -1;
        }
        int rightH = getHeight2(root.right);
        if(leftH >= 0 && rightH >= 0 && Math.abs(leftH-rightH) <= 1) {
            return leftH > rightH ? leftH+1 : rightH+1;
        } else {
            return -1;
        }
    }

    public boolean getPas(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }

        boolean flg = getPas(root.left,node,stack);
        if(flg) {
            return true;
        }
        flg = getPas(root.right,node,stack);
        if(flg) {
            return true;
        }
        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();

        getPas(root,p,stackP);
        getPas(root,q,stackQ);

        int sizeP = stackP.size();
        int sizeQ = stackQ.size();

        int size = sizeP - sizeQ;

        if(size > 0) {
            while (size != 0) {
                stackP.pop();
                size--;
            }
        } else {
            size = sizeQ - sizeP;
            while (size != 0) {
                stackQ.pop();
                size--;
            }
        }

        while (!stackP.isEmpty() && !stackQ.isEmpty()) {
            if(stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            }
            stackP.pop();
            stackQ.pop();
        }
        return null;
    }



//    public static TreeNode createTree(String str) {
//        char ch = str.charAt(i);
//        TreeNode root = null;
//        if(ch != '#') {
//            root = new TreeNode(ch);
//            i++;
//            root.left = createTree(str);
//            root.right = createTree(str);
//        } else{
//            i++;
//        }
//        return root;
    }

